<template>
  <div class="employess">
    <el-card class="top">
      <div class="btn">
        <el-button type="primary">导出</el-button>
        <el-button type="primary" @click="importEX">导入</el-button>
        <el-button type="primary" @click="addBtn">+新增员工</el-button>
      </div>
    </el-card>
    <el-card>
      <el-table style="width: 100%" :data="list">
        <el-table-column label="序号" type="index" sortable>
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" sortable prop="username" />
        <el-table-column label="姓名" sortable prop="staffPhoto">
          <template v-slot="{ row }">
            <div>
              <img
                v-imgerror="fristImg"
                :src="row.staffPhoto"
                alt=""
                style="width: 50px; hight: 50px; object-fit: contain"
                @click="imageOpen(row.staffPhoto)"
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column label="手机号" sortable prop="mobile" />
        <el-table-column label="工号" sortable prop="workNumber" />
        <el-table-column label="聘用形式" sortable prop="formOfEmployment">
          <template v-slot="{ row }">
            {{ row.formOfEmployment | employment }}
          </template>
        </el-table-column>
        <el-table-column label="部门" sortable prop="departmentName" />
        <el-table-column label="入职时间" sortable prop="timeOfEntry" />
        <el-table-column label="状态" sortable prop="enableState">
          <template v-slot="{ row }">
            {{ row.enableState | worState }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="tolook(row.id)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="openRole(row.id)">角色</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 5, 10, 100]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="sizechange"
          @current-change="currentchange"
        />
      </div>
    </el-card>
    <add ref="add" />
    <el-dialog title="二维码" :visible.sync="show" width="400px">
      <div class="canvasImg">
        <canvas ref="canvas" />
      </div>
    </el-dialog>
    <RolePer ref="role" :is-show.sync="isShow" />
  </div>
</template>

<script>
import { sysUser, sysUserDelete } from '@/api/employees'
import employeesData from '@/api/constant/employees'
import add from './components/add.vue'
import cookieJs from 'js-cookie'
import Qrcode from 'qrcode'
import RolePer from './components/rolePermission.vue'
export default {
  components: { add, RolePer },
  // 过滤聘用形式返回值
  filters: {
    employment(srt) {
      const res = employeesData.hireType.find((v) => {
        return (v.id = srt)
      })
      return res ? res.value : '-'
    },
    worState(srt) {
      const res = employeesData.workingState.find((v) => {
        return (v.id = srt)
      })
      return res ? res.value : '-'
    }
  },
  data() {
    return {
      isShow: false,
      show: false,
      fristImg: require('@/assets/common/head.jpg'),
      list: [{}],
      employeesData,
      page: {
        size: 5,
        page: 1,
        total: 100
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    sizechange(size) {
      this.page.size = size
      // cookieJs.set('size', size)
      this.page.page = 1
      this.getData()
    },
    currentchange(page) {
      this.page.page = page
      this.getData()
    },
    async getData() {
      const res = await sysUser(this.page)
      this.page.total = res.data.total
      this.list = res.data.rows
      // console.log(res)
    },
    addBtn() {
      this.$refs.add.show = true
    },
    del(v) {
      this.$confirm('是否删除', '温馨提示')
        .then(async() => {
          await sysUserDelete(v)
          this.$message.success('删除成功！')
          this.page.page = 1
          this.getData()
        })
        .catch(() => {})
    },
    importEX() {
      this.$router.push('/niuzi/import')
    },
    tolook(id) {
      this.$router.push('/employees/look/' + id)
    },
    outPut() {},
    imageOpen(url) {
      this.show = true
      this.$nextTick(() => {
        Qrcode.toCanvas(this.$refs.canvas, url, {
          width: 300,
          hight: 300
        })
      })
    },
    openRole(v) {
      this.isShow = true
      this.$refs.role.getUserInfo(v)
    }
  }
}
</script>

<style lang="scss" scoped>
.employess {
  border: 15px solid #eee;
  .top {
    text-align: right;
    border-bottom: 10px solid #eee;
  }
  .block {
    text-align: right;
  }
  .canvasImg {
    text-align: center;
  }
}
</style>
